package xs.szw.service.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xs.szw.common.annotation.RepeatSubmit;
import xs.szw.common.model.PageResult;
import xs.szw.common.result.Result;
import xs.szw.service.model.vo.UserInfoVo;
import xs.szw.service.model.vo.UserVo;
import xs.szw.service.model.dto.user.*;
import xs.szw.service.service.UserService;
import xs.szw.service.utils.SecurityUtils;

import static xs.szw.common.result.Result.*;

/**
 * @program: xs_oa
 * @description:
 * @author: Songzw
 * @create: 2025-02-10 09:34
 **/

@Tag(name = "用户管理")
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor //推荐构造器注入
public class UserController {


    private final UserService userService;

    @PostMapping("")
    @PreAuthorize("@ss.hasPerm('sys:user:add')")
    @Operation(summary = "创建用户")
    public Result<Object> createUser(@Valid @RequestBody UserDto userDto){
        boolean result = userService.createUser(userDto);
        return judge(result);
    }

    @PutMapping("")
    @PreAuthorize("@ss.hasPerm('sys:user:edit')")
    @Operation(summary = "修改用户")
    public Result<Object> updateUser(@Valid @RequestBody UserUpdateDto UserUpdateDto){
        boolean b = userService.updateUser(UserUpdateDto);
        return judge(b);
    }

    @DeleteMapping("")
    @PreAuthorize("@ss.hasPerm('sys:user:delete')")
    @Operation(summary = "删除用户")
    public Result<Object> deleteUserWithRelations(@RequestParam Long id){
        boolean b = userService.deleteUserWithRelations(id);
        return judge(b);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除用户")
    @Parameters({
            @Parameter(description = "用户ID，多个以英文逗号(,)分割")
    })
    public Result<Object> deleteBatchUserWithRelation(@RequestParam String ids){
        boolean b = userService.deleteBatchUserWithRelation(ids);
        return judge(b);
    }

    @GetMapping("")
    @Operation(summary = "查询单个用户")
    @RepeatSubmit
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public Result<Object> getUser(@RequestParam Long id){
        UserVo userVo = userService.getUser(id);
        return success(userVo,"用户查询成功！");
    }

    @GetMapping("/me")
    @Operation(summary = "获取当前登录用户信息")
    public Result<Object> getCurrentUserInfo(){
        UserInfoVo userInfoVo = userService.getCurrentUserInfo();
        return success(userInfoVo,"获取当前登录用户信息！");
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户列表")
    @Parameters(
            {
                    @Parameter(name = "current",description = "当前页"),
                    @Parameter(name = "size",description = "每页显示数量"),
                    @Parameter(name = "userName",description = "用户名"),
                    @Parameter(name = "token",required = true,description = "jwt令牌",in = ParameterIn.HEADER)
                    //@RequestParam Integer current,@RequestParam Integer size,@RequestParam(required = false) String userName
            }
    )
    public Result<Object> getUserPage(@Valid UserPageDto userPageDto){
        PageResult<UserVo> userPage = userService.getUserPage(userPageDto);
        return success(userPage,"获取分页查询用户列表");
    }

    @PostMapping("/import")
    @Operation(summary = "导入用户")
    public Result<Object> importUser(@RequestParam("file") MultipartFile file){
        //TODO 用户导入
        return null;
    }

    @PutMapping("/password")
    @Operation(summary = "修改密码")
    public Result<Object> updatePassword(@RequestBody PasswordUpdateDto passwordUpdateDto){
        Long currUserId = SecurityUtils.getLoginUserId();
        boolean result = userService.updatePassword(currUserId,passwordUpdateDto);
        return judge(result);
    }

    @PutMapping("/password/reset")
    @Operation(summary = "重置密码")
    @Parameters({
            @Parameter(name = "userId",description = "用户ID"),
            @Parameter(name = "password",description = "新密码")
    })
    public Result<Object> resetPassword(@RequestParam Long userId,@RequestParam String password){
        boolean result = userService.resetPassword(userId,password);
        return judge(result);
    }

    @PostMapping("/send-mobile-code")
    @Operation(summary = "发送手机验证码(绑定或更换手机号)")
    @Parameter(name = "mobile",description = "手机号")
    public Result<Object> sendMobileCode(@RequestParam String mobile){
        //TODO 发送手机验证码
        boolean b =  userService.senMobileCode(mobile);
        return judge(b);
    }

    @PutMapping("/mobile")
    @Operation(summary = "绑定/更改手机号")
    public Result<Object> bindMobileOrUpdateMobile(@Valid @RequestBody
                                                   MobileUpdateDto mobileUpdateDto){
        boolean result =  userService.bindMobileOrUpdateMobile(mobileUpdateDto);
        return judge(result);
    }

    @PostMapping("/send-email-code")
    @Operation(summary = "发送邮箱验证码（绑定或更换邮箱）")
    @Parameter(name = "email",description = "邮箱")
    public Result<Object> sendEmailCode(@RequestParam("email") String email){
        userService.sendEmailCode(email);
        return success();
    }


}
